﻿using AutoMapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using NetTaste;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text.RegularExpressions;
using VM.BLL.Menu;
using VM.Common.DataUntil;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.DAL.System.Code;
using VM.Entity.EntityDto.Goods;
using VM.Entity.EntityDto.Lunbo;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.Goods;
using VM.Entity.EntityMap.ICon;
using VM.Entity.EntityMap.Lunbo;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.Region;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.Goods;
using VM.IBLL.Member;
using VM.IBLL.Menu;
using VM.IBLL.Token;
using VM.IBLL.Topic;
using VM.IDAL;
using VM.IDAL.Goods;


using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.Goods
{
    public class GoodsBLL : BaseBLL, IGoodsBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        private readonly IGoodsDAL _IGoodsDAL;
        private readonly IMapper _IMapper;
        public GoodsBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL, IMenuBLL iMenuBLL, IGoodsDAL iGoodsDAL, IMapper iMapper)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
            _IGoodsDAL = iGoodsDAL;
            _IMapper = iMapper;


        }


        public ApiResult addGoods(addGoodsMainInfoDto obj)

        {
            good_mainInfo objRes = _IMapper.Map<addGoodsMainInfoDto, good_mainInfo>(obj);
            ApiResult res = new ApiResult();
            bool Res = _IGoodsDAL.addGoods(objRes);
            if (Res)
            {
                res.code = 200;
                res.msg = $@"成功插入数据";

            }
            else
            {
                res.code = 403;
                res.msg = $@"成功失败";

            }
            return res;
        }

        public ApiResult addGoodSpec(addGoodSpecDto obj)
        {
            good_spec objRes = _IMapper.Map<addGoodSpecDto, good_spec>(obj);
            ApiResult res = new ApiResult();
            bool Res = _IGoodsDAL.addGoodSpec(objRes);
            if (Res)
            {
                res.code = 200;
                res.msg = $@"成功插入数据";

            }
            else
            {
                res.code = 403;
                res.msg = $@"成功失败";

            }
            return res;
        }

        public ApiDataResult<getGoodsInfoByIdDto> getGoodsInfoById(int goodsId)
        {

            getGoodsInfoByIdDto Res = _IGoodsDAL.getGoodsInfoById(goodsId);
            if (Res.id > 0)
            {
                ApiDataResult<getGoodsInfoByIdDto> res = new ApiDataResult<getGoodsInfoByIdDto>()
                {

                    code = 200,
                    data = Res,
                    msg = "获取成功"
                };
                return res;
            }
            else
            {
                ApiDataResult<getGoodsInfoByIdDto> res = new ApiDataResult<getGoodsInfoByIdDto>()
                {

                    code = 410,
                    data = null,
                    msg = "数据不存在，请检查"
                };
                return res;
            }
        }

        /// <summary>
        /// 根据商品类别获取商品类表，分页数据
        /// </summary>
        /// <param name="goodsTypeId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ApiDataResult<PagingData<getGoodsListByTypeIdDto>> getGoodsListByTypeId(int goodsTypeId, int pageIndex, int pageSize)
        {
            PagingData<getGoodsListByTypeIdDto> List = _IGoodsDAL.getGoodsListByTypeId(goodsTypeId, pageIndex, pageSize);
            ApiDataResult<PagingData<getGoodsListByTypeIdDto>> res = new ApiDataResult<PagingData<getGoodsListByTypeIdDto>>()
            {

                code = 200,
                msg = "获取成功",
                data = List
            };
            return res;
        }

        public ApiDataResult<List<getLunboListDto>> getGoodsLunBoList(int lunBoTypeId)
        {
            List<getLunboListDto> menuList = _IGoodsDAL.getGoodsLunBoList(lunBoTypeId);
            //_ISqlSuggarDAL.QueryPageList<sys_menu>(PageSize, PageIndex);

            ApiDataResult<List<getLunboListDto>> res = new ApiDataResult<List<getLunboListDto>>();


            if (menuList.Count > 0)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = menuList;
            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }
            return res;



        }







        ///// <summary>
        ///// 获取列表
        ///// </summary>
        ///// <param name="name"></param>
        ///// <param name="userId"></param>
        ///// <param name="pageSize"></param>
        ///// <param name="pageIndex"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public ApiDataResult<PagingData<getGoodsTypeListDto>> getGoodsTypeList(string? name, int? userId, int pageSize, int pageIndex)
        //{
        //    ApiDataResult<PagingData<getGoodsTypeListDto>> res = new ApiDataResult<PagingData<getGoodsTypeListDto>>();

        //    PagingData<getGoodsTypeListDto> dataRes= _IGoodsTypeDAL.getGoodsTypeList(   name, userId,  pageSize,  pageIndex);


        //        res.code = 200;
        //        res.data = dataRes;
        //        res.msg = "查询成功";
        //    return res;

        //}


    }
}




